import request from './request'

// 通用接口
export const commonAPI = {
  // 获取下拉选项
  getOption: (tableName, columnName, conditionColumn, conditionValue) => {
    return request({
      url: `/option/${tableName}/${columnName}`,
      method: 'get',
      params: { conditionColumn, conditionValue }
    })
  },
  
  // 根据条件获取单条记录
  getFollowByOption: (tableName, columnName, columnValue) => {
    return request({
      url: `/follow/${tableName}/${columnName}`,
      method: 'get',
      params: { columnValue }
    })
  },
  
  // 审核
  sh: (tableName, data) => {
    return request({
      url: `/sh/${tableName}`,
      method: 'post',
      data
    })
  },
  
  // 提醒
  remind: (tableName, columnName, type, data) => {
    return request({
      url: `/remind/${tableName}/${columnName}/${type}`,
      method: 'get',
      params: data
    })
  },
  
  // 单列求和
  cal: (tableName, columnName) => {
    return request({
      url: `/cal/${tableName}/${columnName}`,
      method: 'get'
    })
  },
  
  // 分组统计
  group: (tableName, columnName) => {
    return request({
      url: `/group/${tableName}/${columnName}`,
      method: 'get'
    })
  },
  
  // 按值统计
  value: (tableName, xColumnName, yColumnName) => {
    return request({
      url: `/value/${tableName}/${xColumnName}/${yColumnName}`,
      method: 'get'
    })
  },
  
  // 按时间统计
  valueDay: (tableName, xColumnName, yColumnName, timeStatType) => {
    return request({
      url: `/value/${tableName}/${xColumnName}/${yColumnName}/${timeStatType}`,
      method: 'get'
    })
  }
}

// 用户相关接口
export const userAPI = {
  // 登录
  login: (tableName, username, password) => {
    return request({
      url: `/${tableName}/login`,
      method: 'post',
      params: { username, password }
    })
  },
  
  // 注册
  register: (tableName, data) => {
    return request({
      url: `/${tableName}/register`,
      method: 'post',
      data
    })
  },
  
  // 退出
  logout: (tableName) => {
    return request({
      url: `/${tableName}/logout`,
      method: 'get'
    })
  },
  
  // 重置密码
  resetPassword: (tableName, username) => {
    return request({
      url: `/${tableName}/resetPass`,
      method: 'post',
      params: { username }
    })
  },
  
  // 获取用户列表（分页）
  getPage: (tableName, params) => {
    return request({
      url: `/${tableName}/page`,
      method: 'get',
      params
    })
  },
  
  // 获取用户列表
  getList: (tableName, params) => {
    return request({
      url: `/${tableName}/list`,
      method: 'get',
      params
    })
  },
  
  // 获取用户信息
  getInfo: (tableName, id) => {
    return request({
      url: `/${tableName}/info/${id}`,
      method: 'get'
    })
  },
  
  // 获取当前用户信息
  getSession: (tableName) => {
    return request({
      url: `/${tableName}/session`,
      method: 'get'
    })
  },
  
  // 保存用户
  save: (tableName, data) => {
    return request({
      url: `/${tableName}/save`,
      method: 'post',
      data
    })
  },
  
  // 更新用户信息
  update: (tableName, data) => {
    return request({
      url: `/${tableName}/update`,
      method: 'post',
      data
    })
  },
  
  // 更新用户头像
  updateImg: (tableName, file) => {
    const formData = new FormData()
    formData.append('file', file)
    return request({
      url: `/${tableName}/updateImg`,
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },
  
  // 删除用户
  delete: (tableName, ids) => {
    return request({
      url: `/${tableName}/delete`,
      method: 'post',
      data: ids
    })
  }
}

// 宿舍分配接口
export const dormitoryAPI = {
  // 获取宿舍分配列表（分页）
  getPage: (params) => {
    return request({
      url: '/sushefenpei/page',
      method: 'get',
      params
    })
  },
  
  // 获取宿舍分配列表
  getList: (params) => {
    return request({
      url: '/sushefenpei/list',
      method: 'get',
      params
    })
  },
  
  // 获取宿舍分配信息
  getInfo: (id) => {
    return request({
      url: `/sushefenpei/info/${id}`,
      method: 'get'
    })
  },
  
  // 保存宿舍分配
  save: (data) => {
    return request({
      url: '/sushefenpei/save',
      method: 'post',
      data
    })
  },
  
  // 更新宿舍分配
  update: (data) => {
    return request({
      url: '/sushefenpei/update',
      method: 'post',
      data
    })
  },
  
  // 删除宿舍分配
  delete: (ids) => {
    return request({
      url: '/sushefenpei/delete',
      method: 'post',
      data: ids
    })
  }
}

// 宿舍报修接口
export const repairAPI = {
  // 获取报修列表（分页）
  getPage: (params) => {
    return request({
      url: '/sushebaoxiu/page',
      method: 'get',
      params
    })
  },
  
  // 获取报修列表
  getList: (params) => {
    return request({
      url: '/sushebaoxiu/list',
      method: 'get',
      params
    })
  },
  
  // 获取报修信息
  getInfo: (id) => {
    return request({
      url: `/sushebaoxiu/info/${id}`,
      method: 'get'
    })
  },
  
  // 保存报修
  save: (data) => {
    return request({
      url: '/sushebaoxiu/save',
      method: 'post',
      data
    })
  },
  
  // 更新报修
  update: (data) => {
    return request({
      url: '/sushebaoxiu/update',
      method: 'post',
      data
    })
  },
  
  // 删除报修
  delete: (ids) => {
    return request({
      url: '/sushebaoxiu/delete',
      method: 'post',
      data: ids
    })
  }
}

// 宿管接口
export const dormManagerAPI = {
  // 获取宿管列表（分页）
  getPage: (params) => {
    return request({
      url: '/suguan/page',
      method: 'get',
      params
    })
  },
  
  // 获取宿管列表
  getList: (params) => {
    return request({
      url: '/suguan/list',
      method: 'get',
      params
    })
  },
  
  // 获取宿管信息
  getInfo: (id) => {
    return request({
      url: `/suguan/info/${id}`,
      method: 'get'
    })
  },
  
  // 保存宿管
  save: (data) => {
    return request({
      url: '/suguan/save',
      method: 'post',
      data
    })
  },
  
  // 更新宿管
  update: (data) => {
    return request({
      url: '/suguan/update',
      method: 'post',
      data
    })
  },
  
  // 删除宿管
  delete: (ids) => {
    return request({
      url: '/suguan/delete',
      method: 'post',
      data: ids
    })
  }
}

// 学生接口
export const studentAPI = {
  // 获取学生列表（分页）
  getPage: (params) => {
    return request({
      url: '/xuesheng/page',
      method: 'get',
      params
    })
  },
  
  // 获取学生列表
  getList: (params) => {
    return request({
      url: '/xuesheng/list',
      method: 'get',
      params
    })
  },
  
  // 获取学生信息
  getInfo: (id) => {
    return request({
      url: `/xuesheng/info/${id}`,
      method: 'get'
    })
  },
  
  // 保存学生
  save: (data) => {
    return request({
      url: '/xuesheng/save',
      method: 'post',
      data
    })
  },
  
  // 更新学生
  update: (data) => {
    return request({
      url: '/xuesheng/update',
      method: 'post',
      data
    })
  },
  
  // 删除学生
  delete: (ids) => {
    return request({
      url: '/xuesheng/delete',
      method: 'post',
      data: ids
    })
  }
}

// 文件上传接口
export const fileAPI = {
  // 文件上传
  upload: (file) => {
    const formData = new FormData()
    formData.append('file', file)
    return request({
      url: '/file/upload',
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },
  
  // 获取文件访问路径
  getFileUrl: (fileName) => {
    return `/springbootyyh3w/file/download?fileName=${fileName}`
  }
}

// 公告通知接口
export const announcementAPI = {
  // 获取公告列表（分页）
  getPage: (params) => {
    return request({
      url: '/xinwengonggao/page',
      method: 'get',
      params
    })
  },
  
  // 获取公告列表
  getList: (params) => {
    return request({
      url: '/xinwengonggao/list',
      method: 'get',
      params
    })
  },
  
  // 获取公告详情
  getInfo: (id) => {
    return request({
      url: `/xinwengonggao/info/${id}`,
      method: 'get'
    })
  },
  
  // 获取公告详情（前端）
  getDetail: (id) => {
    return request({
      url: `/xinwengonggao/detail/${id}`,
      method: 'get'
    })
  },
  
  // 保存公告
  save: (data) => {
    return request({
      url: '/xinwengonggao/save',
      method: 'post',
      data
    })
  },
  
  // 添加公告（前端）
  add: (data) => {
    return request({
      url: '/xinwengonggao/add',
      method: 'post',
      data
    })
  },
  
  // 更新公告
  update: (data) => {
    return request({
      url: '/xinwengonggao/update',
      method: 'post',
      data
    })
  },
  
  // 删除公告
  delete: (ids) => {
    return request({
      url: '/xinwengonggao/delete',
      method: 'post',
      data: ids
    })
  }
}

// 修改登录页使用API服务
export default {
  commonAPI,
  userAPI,
  dormitoryAPI,
  repairAPI,
  dormManagerAPI,
  studentAPI,
  fileAPI,
  announcementAPI
} 